//
//  MapDetailsViewController.m
//  YiBaoStore
//
//  Created by 吕清毅 on 2018/12/1.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "MapDetailsViewController.h"
#import <AMapSearchKit/AMapSearchKit.h>
#import "MapTableViewCell.h"

@interface MapDetailsViewController ()<AMapSearchDelegate,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic, strong) UIView *headView;
@property (nonatomic, strong) UITextField *searchTF;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) AMapSearchAPI *search; //搜索api
@property (nonatomic, strong) NSMutableArray *addressArray; //周边地址数组
@end

@implementation MapDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setHeadView];
    [self setTable];
}

-  (void)setHeadView
{
    self.headView = [UIView new];
    self.headView.frame = CGRectMake(0, 0, SCREENWIDTH, 120+TopStatusHeight);
    self.headView.backgroundColor = goldColor;
    [self.view addSubview:self.headView];
    
    UIImageView *leftIV = [UIImageView new];
    leftIV.frame = CGRectMake(15, TopStatusHeight+35, 18, 14);
    leftIV.image = [UIImage imageNamed:@"back"];
    [self.headView addSubview:leftIV];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(8, TopStatusHeight+28, 32, 28);
    leftBtn.backgroundColor = ClearColor;
    [leftBtn addTarget:self action:@selector(leftBtnBackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.headView addSubview:leftBtn];
    
    UITextField *searchTF = [UITextField new];
    searchTF.frame = CGRectMake(15, self.headView.height-50, SCREENWIDTH-30, 40);
    searchTF.backgroundColor = TotalWhite;
    searchTF.alpha = 0.5;
    searchTF.layer.cornerRadius = 20;
    searchTF.placeholder = @"小区/写字楼/街道等";
    searchTF.returnKeyType = UIReturnKeySearch;
    searchTF.delegate = self;
    [searchTF addTarget:self action:@selector(textChange) forControlEvents:UIControlEventEditingChanged];
    self.searchTF = searchTF;
    [self.headView addSubview:searchTF];
    
    UIView *View = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    UIImageView *leftSearch = [UIImageView new];
    leftSearch.frame = CGRectMake(20, 11, 18, 18);
    leftSearch.image = [UIImage imageNamed:@"mapSearch"];
    [View addSubview:leftSearch];
    searchTF.leftViewMode = UITextFieldViewModeAlways;
    searchTF.leftView = View;
    
    //地图搜索api
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
}

- (void)setTable
{
    if (self.tableView == nil) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,self.self.headView.bottom, SCREENWIDTH, SCREENHEIGHT-self.headView.bottom) style:UITableViewStylePlain];
        self.tableView.backgroundColor = TotalWhite;
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
        [self.tableView registerClass:[MapTableViewCell class] forCellReuseIdentifier:@"MapTableViewCell"];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.rowHeight = 52;
        self.tableView.tableFooterView = [UIView new];
        [TTKUtil setAutomaticallyAdjustsScrollViewInsetsWidth:self.tableView withView:self];
        //self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getNewData)];
        [self.view addSubview:self.tableView];
    }
}

- (void)textChange
{
    //[self.searchTF resignFirstResponder];
    if(self.searchTF.text.length == 0) {
        return;
    }
    //开始搜索
    [self searchPoiByKeyword:self.searchTF.text];
}

/* 根据关键字来搜索POI. */
- (void)searchPoiByKeyword:(NSString *)keyword
{
    AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
    request.keywords = keyword;
    //    request.keywords            = @"北京大学";
    //    request.city                = @"北京";
    //    request.types               = @"高等院校";
    //    request.requireExtension    = YES;
    //
    //    /*  搜索SDK 3.2.0 中新增加的功能，只搜索本城市的POI。*/
    request.cityLimit = YES;
    request.requireExtension = YES;
    request.requireSubPOIs = YES;
    [self.search AMapPOIKeywordsSearch:request];
}

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    [self.addressArray removeAllObjects];
    if (response.pois.count == 0)
    {
        return;
    }
    self.addressArray = [NSMutableArray arrayWithArray:response.pois];
    [self.tableView reloadData];
}

#pragma mark -- tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.addressArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MapTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[MapTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MapTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.indexPath = indexPath;
    AMapPOI *model = self.addressArray[indexPath.row];
    [cell getModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AMapPOI *model = self.addressArray[indexPath.row];
    if (self.callBack) {
        self.callBack(model.name,model.province,model.city,model.district,model.address,model.location.latitude,model.location.longitude);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSMutableArray *)addressArray
{
    if (!_addressArray) {
        _addressArray = [NSMutableArray array];
    }
    return _addressArray;
}


- (void)leftBtnBackClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
